// page/diary-search-list/index.js
const util = require('../../util/util')
const db = wx.cloud.database();
const app = getApp();
const MAX_LIMIT = 10
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startRefresh: false,
    refreshText: '刷新成功',
    requesting: false,
		end: false,
		emptyShow: false,
    enableBackToTop: true,
		refreshSize: 70,
		topSize: 0,
		bottomSize: 70,
    color: '#4CA1AF',

    startDate: '',
    endDate: '',
    diaries: [],
    diary_search_page: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    this.setData({
      startDate: e.startDate,
      endDate: e.endDate,
      refreshText: '搜索成功'
    })
    wx.setNavigationBarTitle({
      title: this.data.startDate + ' ~ ' + this.data.endDate
  })
    this.searchMore()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  refresh() {
    console.log("下拉刷新事件开始")
    this.setData({
      refreshText: '刷新成功'
    })
    this.searchMore();
  },

  // 搜索操作
  searchMore:function() {
    var that = this
    that.setData({
			requesting: true
		}); 
    
    that.setData({
      requesting: false
    });
    if (this.data.end) {
      return
    }
    var startD = that.data.startDate.replace(/-/g, "/")
    var endD = that.data.endDate.replace(/-/g, "/")
    // 定时任务0.5s后执行
    const _ = db.command    
    setTimeout(function () {
      var page = that.data.diary_search_page;
      db.collection('diary').where({
        createDate: _.gte(new Date(startD)).and(_.lte(new Date(endD)))
      }).orderBy('createTime','desc')
      .skip((page-1) * MAX_LIMIT)
      .limit(MAX_LIMIT)
      .get({
        success: function(res) {
          let diary_data = res.data
          // res.data 是包含以上定义的两条记录的数组
          console.log("事件刷新",res.data)
          // 如果查询不到数据，则判定加载完成
          if (diary_data == null || diary_data.length < 1) {
            that.setData({     
              end: true
            });
            return;
          }
          // 页面数+1
          that.setData({
            diary_search_page: that.data.diary_search_page + 1
          });
          // 处理日记内容显示，默认只取前二十个字
          for (let i = 0; i < diary_data.length; i++) {
            diary_data[i].minicontent = diary_data[i].content.substring(0, 20);
          }
          // 新加载的数据
          var new_data = diary_data;
          // 原总的记录数
          var total_data = that.data.diaries;
          // 数据累加
          total_data = total_data.concat(new_data);
          that.setData({
            diaries: total_data
          });
        }
      })
    }, 500);
  },

  // 加载更多
  loadMore: function () {
    console.log("上拉触底事件")
    this.setData({
			requesting: true
		});
    if (this.data.end) {
      return;
    }
    var that = this;
    var startD = that.data.startDate.replace(/-/g, "/")
    var endD = that.data.endDate.replace(/-/g, "/")
    const _ = db.command
    // 定时任务0.5s后执行
    setTimeout(function () {
      var page = that.data.diary_search_page;
      db.collection('diary').where({
        createDate: _.gte(new Date(startD)).and(_.lte(new Date(endD)))
      }).orderBy('createTime','desc')
      .skip((page-1) * MAX_LIMIT)
      .limit(MAX_LIMIT)
      .get({
        success: function(res) {
          let diary_data = res.data
          // res.data 是包含以上定义的两条记录的数组
          console.log("上拉触底加载：",res.data)
          // 如果查询不到数据，则判定加载完成
          if (diary_data == null || diary_data.length < 1) {   
            that.setData({     
              end: true
            });         
            return;
          }
          // 页面数+1
          that.setData({
            diary_search_page: that.data.diary_search_page + 1
          });
          // 处理日记内容显示，默认只取前二十个字
          for (let i = 0; i < diary_data.length; i++) {
            diary_data[i].minicontent = diary_data[i].content.substring(0, 20);
          }
          // 新加载的数据
          var new_data = diary_data;
          // 原总的记录数
          var total_data = that.data.diaries;
          // 数据累加
          total_data = total_data.concat(new_data);
          that.setData({
            diaries: total_data,
            end: false
          });
        }
      })
    }, 1000);
  },

  // 查看日记详情
  showDiary: function (e) {
    var index = e.currentTarget.dataset.diary;
    var that = this;
    var diary_data = that.data.diaries[index];
    var ss = JSON.stringify(diary_data)
    util.date2JSON()
    console.log(ss);
    wx.setStorage({
      key: 'diary',
      data: diary_data
    });

    // console.log(index);
    that.jump2DiaryEdit()
  },

  jump2DiaryEdit() {
    wx.navigateTo({
      url: '/page/diary-edit/index'
    });
  },

})